package cn.yh.hospitalappointmentsystemdemo.service.impl;

import cn.yh.hospitalappointmentsystemdemo.domain.entity.User;
import cn.yh.hospitalappointmentsystemdemo.domain.vo.AdminVo;
import cn.yh.hospitalappointmentsystemdemo.domain.vo.DoctorVo;
import cn.yh.hospitalappointmentsystemdemo.domain.vo.PatientVo;
import cn.yh.hospitalappointmentsystemdemo.mapper.AdminsMapper;
import cn.yh.hospitalappointmentsystemdemo.mapper.DoctorsMapper;
import cn.yh.hospitalappointmentsystemdemo.mapper.PatientsMapper;
import cn.yh.hospitalappointmentsystemdemo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private PatientsMapper patientsMapper;
    @Autowired
    private DoctorsMapper doctorsMapper;
    @Autowired
    private AdminsMapper adminsMapper;
    @Override
    public User checkLogin(User user) {

        User userDb = null;

        if (user.getRole() == 1) {
            userDb = patientsMapper.loginByUsername(user.getUsername());
        } else if (user.getRole() == 2) {
            userDb = doctorsMapper.loginByUsername(user.getUsername());
        } else if (user.getRole() == 3) {
            userDb = adminsMapper.loginByUsername(user.getUsername());
        } else {
            throw new IllegalArgumentException("非法的用户角色");
        }

//        if (userDb == null) {
//            throw new RuntimeException("用户名不存在");
//        }

        return userDb;
    }

    @Override
    public Object getUserVo(Long userId, Long role) {
//        Object userVo = new Object();
        System.out.println("role="+role);
        if (role==1){
            PatientVo patientVo = patientsMapper.selectById(userId);
            patientVo.setRole(role);
            return patientVo;

        }
        if (role==2){
            DoctorVo doctorVo = doctorsMapper.selectById(userId);
            doctorVo.setRole(role);
            return doctorVo;
        }
        if (role==3){
            AdminVo adminVo = adminsMapper.selectById(userId);
            adminVo.setRole(role);
            return adminVo;
        }
        return null;
    }

}
